package com.cheung.leetcode.arr;

import  org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.arr
 * @Author :  cheungming
 * @CreateTime : 2024-07-15 21:31:43
 * @Description : 189. 旋转数组
 * @Version : 1.0
 */
public class Code189Test {

    public void rotate(int[] nums, int k) {
        int len = nums.length;
        k = k % len;
        int[] tmp = new int[k];
        int index = k;
        while (index > 0) {
            // 将最后一个元素依次放入temp的最后
            tmp[index - 1] = nums[len - 1];
            index--;
            len--;
        }

        // 将nums的元素依次往后移动k位
        for (int i = nums.length - 1; i >= k; i--) {
            nums[i] = nums[i - k];
        }
        // 将temp的元素依次放入nums的开头
        System.arraycopy(tmp, 0, nums, 0, k);
    }

    @Test
    public void test1() {
        int[] nums = {1, 2, 3, 4, 5, 6, 7};
        rotate(nums, 3);

        int[] result = {5, 6, 7, 1, 2, 3, 4};
        assert Arrays.equals(nums, result);
    }
}
